package com.yakaten.utils;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;

public class Y {

	static String Y_SHOP_URL = "subvenire";

	static String Y_CATEGORY_URL = "http://store.shopping.yahoo.co.jp/{0}/{1}.html";

	static String Y_PAGE_ENCODING = "EUC_JP";

	static String Y_USERAGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.33 Safari/535.11";

	static String Y_ITEM_URL = "http://store.shopping.yahoo.co.jp/{0}/{1}.html";

	static String Y_ITEM_IMG_SAVE_PATH = "C:/Users/ryu/Desktop/yahoo1/";

	static String Y_ITEM_IMG_URL_1 = "http://item.shopping.c.yimg.jp/i/l/{0}_{1}";

	static Map<String, String> FILE_TYPE = new HashMap<String, String>();

	static {
		FILE_TYPE.put("image/jpeg", ".jpg");
		FILE_TYPE.put("image/jpg", ".jpg");
		FILE_TYPE.put("image/gif", ".gif");

	}

	protected static final String REGEX_Y_CATEGORY_PAGE_COUNT_DIV = "<div class=\"pageNavi\">(.+?)</div>";
	protected static final Pattern PATTERN_Y_CATEGORY_PAGE_COUNT_DIV = Pattern
			.compile(REGEX_Y_CATEGORY_PAGE_COUNT_DIV, Pattern.MULTILINE);

	protected static final String REGEX_Y_CATEGORY_PAGE_COUNT_ELNUM = "<td class=\"elNum\"><a.+?/a></td>";
	protected static final Pattern PATTERN_Y_CATEGORY_PAGE_COUNT_ELNUM = Pattern
			.compile(REGEX_Y_CATEGORY_PAGE_COUNT_ELNUM, Pattern.MULTILINE);

	protected static final String REGEX_Y_CATEGORY_PAGE_ITEMLIST_TABLE = "<div id=\"CentItemList1\">.+?<table.+?class=\"list\">(.+?)</table>";
	protected static final Pattern PATTERN_Y_CATEGORY_PAGE_ITEMLIST_TABLE = Pattern
			.compile(REGEX_Y_CATEGORY_PAGE_ITEMLIST_TABLE, Pattern.MULTILINE);

	protected static final String REGEX_Y_CATEGORY_PAGE_ITEMLIST_ITEM_ID = "<th><a href=\"/"
			+ Y_SHOP_URL + "/(.+?).html\">";
	protected static final Pattern PATTERN_Y_CATEGORY_PAGE_ITEMLIST_ITEM_ID = Pattern
			.compile(REGEX_Y_CATEGORY_PAGE_ITEMLIST_ITEM_ID, Pattern.MULTILINE);

	protected static final String REGEX_Y_ITEM_PAGE_DETAIL_AREA = "<!-- CENT ITEM CAPTION1 -->(.+?)<!-- /CENT ITEM CAPTION1 -->";
	protected static final Pattern PATTERN_Y_ITEM_PAGE_DETAIL_AREA = Pattern
			.compile(REGEX_Y_ITEM_PAGE_DETAIL_AREA, Pattern.MULTILINE);

	protected static final String REGEX_Y_ITEM_PAGE_DETAIL_AREA1 = "<!-- CENT ITEM ADDITIONAL1 -->(.+?)<!-- /CENT ITEM ADDITIONAL1 -->";
	protected static final Pattern PATTERN_Y_ITEM_PAGE_DETAIL_AREA1 = Pattern
			.compile(REGEX_Y_ITEM_PAGE_DETAIL_AREA1, Pattern.MULTILINE);

	protected static final String REGEX_Y_ITEM_PAGE_DETAIL_AREA_IMGURL = "<img.+?data-original=\"(.+?)\"";
	protected static final Pattern PATTERN_Y_ITEM_PAGE_DETAIL_AREA_IMGURL = Pattern
			.compile(REGEX_Y_ITEM_PAGE_DETAIL_AREA_IMGURL, Pattern.MULTILINE);

	public static void main(String args[]) {
		// for(String tmp:new Y2C()
		// .getCategoryItemIdListByUrl("http://store.shopping.yahoo.co.jp/subvenire/a5d0a5c3a5.html")){
		// System.out.println(tmp);
		// }

		// for (String tmp : new Y2C().getCategoryItemIdList("bbfeb7d7")) {
		// System.out.println(tmp);
		// }

//		 new Y().getItemInfo("3016-8colour");
		// new Y2C().saveBaseImg("gy002");

		// for (String tmp : new Y2C().getDetailImgUrlList("gg2")) {
		// System.out.println(tmp);
		// }
		//
		new Y().getCategoryItemInfo("bae2c9db");

	}

	public boolean getCategoryItemInfo(String categoryId) {
		boolean result = false;
		Y_ITEM_IMG_SAVE_PATH = Y_ITEM_IMG_SAVE_PATH+categoryId+"/";
		for (String itemId : getCategoryItemIdList(categoryId)) {
			getItemInfo(itemId);
		}
		return result;

	}

	public boolean saveImg(String imgUrl, File savePath, String fileName) {

		HttpURLConnection conn = null;
		DataInputStream dataInStream = null;
		DataOutputStream dataOutStream = null;
		try {
			URL url = new URL(imgUrl);

			conn = (HttpURLConnection) url.openConnection();
			conn.setAllowUserInteraction(false);
			conn.setInstanceFollowRedirects(true);
			conn.setRequestMethod("GET");
			conn.connect();

			int httpStatusCode = conn.getResponseCode();

			if (httpStatusCode != HttpURLConnection.HTTP_OK) {
				return false;
			}

			Map<String, List<String>> headers = conn.getHeaderFields();

			String contentType = headers.get("Content-Type").get(0);

			if (contentType == null || contentType.length() == 0) {
				return false;
			}

			String fileType = FILE_TYPE.get(contentType);
			if (fileType == null || fileType.length() == 0) {
				return false;
			}

			// Input Stream
			dataInStream = new DataInputStream(conn.getInputStream());

			// Output Stream
			dataOutStream = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(savePath.getAbsoluteFile() + "/"
							+ fileName + fileType)));

			// Read Data
			byte[] b = new byte[4096];
			int readByte = 0;

			while (-1 != (readByte = dataInStream.read(b))) {
				dataOutStream.write(b, 0, readByte);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (dataInStream != null) {
				try {
					dataInStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				conn.disconnect();
			}

			if (dataOutStream != null) {
				try {
					dataOutStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}

	public boolean getItemInfo(String itemId) {
		boolean result = true;

		saveBaseImg(itemId);
		saveDetailImg(itemId);

		return result;

	}

	public boolean saveBaseImg(String itemId) {
		String imgBaseUrl = Y_ITEM_IMG_URL_1.replace("{0}", Y_SHOP_URL)
				.replace("{1}", itemId);

		File itemImgFolder = new File(Y_ITEM_IMG_SAVE_PATH + itemId);

		if (!itemImgFolder.exists()) {
			itemImgFolder.mkdirs();
		}

		saveImg(imgBaseUrl, itemImgFolder, itemId);
		saveImg(imgBaseUrl + "_1", itemImgFolder, itemId + "_1");
		saveImg(imgBaseUrl + "_2", itemImgFolder, itemId + "_2");
		saveImg(imgBaseUrl + "_3", itemImgFolder, itemId + "_3");
		saveImg(imgBaseUrl + "_4", itemImgFolder, itemId + "_4");
		saveImg(imgBaseUrl + "_5", itemImgFolder, itemId + "_5");

		return true;

	}

	public boolean saveDetailImg(String itemId) {

		File itemImgFolder = new File(Y_ITEM_IMG_SAVE_PATH + itemId);

		if (!itemImgFolder.exists()) {
			itemImgFolder.mkdirs();
		}

		int i = 1;
		for (String detailUrl : getDetailImgUrlList(itemId)) {
			saveImg(detailUrl, itemImgFolder, itemId + "_d_" + (i++));
		}
		return false;
	}

	public List<String> getDetailImgUrlList(String itemId) {
		List<String> detailImgUrlList = new ArrayList<String>();
		HttpURLConnection connection = null;
		BufferedReader bufferedReader = null;
		OutputStreamWriter out = null;
		try {
			String url = Y_ITEM_URL.replace("{0}", Y_SHOP_URL).replace("{1}",
					itemId);

			URL loginUrl = new URL(url);
			connection = (HttpURLConnection) loginUrl.openConnection();
			connection.setRequestProperty("User-Agent", Y_USERAGENT);

			connection.connect();

			InputStream urlStream = connection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(
					urlStream, Y_PAGE_ENCODING));
			String ss = null;
			StringBuffer htmlBuffer = new StringBuffer();
			while ((ss = bufferedReader.readLine()) != null) {
				htmlBuffer.append(ss);
			}

			if (htmlBuffer != null) {

				Matcher matcherDiv = PATTERN_Y_ITEM_PAGE_DETAIL_AREA
						.matcher(htmlBuffer);

				if (matcherDiv.find()) {

					Matcher matcherDetailImgUrl = PATTERN_Y_ITEM_PAGE_DETAIL_AREA_IMGURL
							.matcher(matcherDiv.group(1));
					while (matcherDetailImgUrl.find()) {
						detailImgUrlList.add(matcherDetailImgUrl.group(1));
					}
				}

				Matcher matcherDiv1 = PATTERN_Y_ITEM_PAGE_DETAIL_AREA1
						.matcher(htmlBuffer);

				if (matcherDiv1.find()) {

					Matcher matcherDetailImgUrl = PATTERN_Y_ITEM_PAGE_DETAIL_AREA_IMGURL
							.matcher(matcherDiv1.group(1));
					while (matcherDetailImgUrl.find()) {
						detailImgUrlList.add(matcherDetailImgUrl.group(1));
					}
				}

			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return detailImgUrlList;
	}

	public List<String> getCategoryItemIdList(String categoryId) {
		List<String> categoryItemIdList = new ArrayList<String>();

		int pageCount = new Y().getCategoryPageCount(categoryId);

		for (String categoryURL : getAllCategoryPageUrl(categoryId, pageCount)) {
			categoryItemIdList.addAll(getCategoryItemIdListByUrl(categoryURL));
		}

		return categoryItemIdList;
	}

	public List<String> getCategoryItemIdListByUrl(String categoryURL) {
		List<String> categoryItemIdList = new ArrayList<String>();
		HttpURLConnection connection = null;
		BufferedReader bufferedReader = null;
		OutputStreamWriter out = null;
		try {
			URL loginUrl = new URL(categoryURL);
			connection = (HttpURLConnection) loginUrl.openConnection();
			connection.setRequestProperty("User-Agent", Y_USERAGENT);

			connection.connect();

			InputStream urlStream = connection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(
					urlStream, Y_PAGE_ENCODING));
			String ss = null;
			StringBuffer htmlBuffer = new StringBuffer();
			while ((ss = bufferedReader.readLine()) != null) {
				htmlBuffer.append(ss);
			}

			if (htmlBuffer != null) {
				// IOUtils.write(htmlBuffer, new
				// FileOutputStream("c:/yahoo.html"),Y_PAGE_ENCODING);

				Matcher matcherDiv = PATTERN_Y_CATEGORY_PAGE_ITEMLIST_TABLE
						.matcher(htmlBuffer);

				if (matcherDiv.find()) {

					Matcher matcherItemId = PATTERN_Y_CATEGORY_PAGE_ITEMLIST_ITEM_ID
							.matcher(matcherDiv.group(1));
					while (matcherItemId.find()) {
						categoryItemIdList.add(matcherItemId.group(1));
					}
				}

			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return categoryItemIdList;
	}

	public List<String> getAllCategoryPageUrl(String categoryId, int PageCount) {
		List<String> allCategorypageUrl = new ArrayList<String>();
		String url = Y_CATEGORY_URL.replace("{0}", Y_SHOP_URL).replace("{1}",
				categoryId);
		for (int i = 0; i < PageCount; i++) {
			int b = i * 30 + 1;
			int n = 30;
			String workUrl = url.concat("?b=").concat("" + b).concat("&n=")
					.concat("" + n);
			allCategorypageUrl.add(workUrl);
		}
		return allCategorypageUrl;
	}

	public int getCategoryPageCount(String categoryId) {
		int pageCount = 1;

		String url = Y_CATEGORY_URL.replace("{0}", Y_SHOP_URL).replace("{1}",
				categoryId);

		HttpURLConnection connection = null;
		BufferedReader bufferedReader = null;
		OutputStreamWriter out = null;
		try {
			URL loginUrl = new URL(url);
			connection = (HttpURLConnection) loginUrl.openConnection();
			connection.setRequestProperty("User-Agent", Y_USERAGENT);

			connection.connect();

			InputStream urlStream = connection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(
					urlStream, Y_PAGE_ENCODING));
			String ss = null;
			StringBuffer htmlBuffer = new StringBuffer();
			while ((ss = bufferedReader.readLine()) != null) {
				htmlBuffer.append(ss);
			}

			if (htmlBuffer != null) {
				// IOUtils.write(htmlBuffer, new
				// FileOutputStream("c:/yahoo.html"),Y_PAGE_ENCODING);

				Matcher matcherDiv = PATTERN_Y_CATEGORY_PAGE_COUNT_DIV
						.matcher(htmlBuffer);

				if (matcherDiv.find()) {
					String divString = "";
					divString = matcherDiv.group(1);

					if (divString != null && !"".equals(divString)) {
						Matcher matcherElnum = PATTERN_Y_CATEGORY_PAGE_COUNT_ELNUM
								.matcher(divString);
						while (matcherElnum.find()) {
							pageCount++;
						}
					}
				}

			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return pageCount;
	}
}
